package me.fung.rem.entry

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.ResourceLoader
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.*
import me.fung.keen.app.nav.HOME
import me.fung.keen.component.message.MessageContent
import me.fung.rem.app.GlobalState
import me.fung.rem.app.common.loading
import me.fung.rem.app.main.init
import me.fung.rem.app.navigation
import me.fung.rem.bean.repo.*

private const val TITLE: String = "Rem ^_^"

fun application() {
    application {
        var waitLoading: Boolean by remember { mutableStateOf(true) }
        if (waitLoading) {
            load()
            LaunchedEffect(Unit) {
                init()
                waitLoading = false
            }
        } else {
            main()
        }

    }
}

@Composable
fun ApplicationScope.load() {
    Window(
        onCloseRequest = ::exitApplication,
        state = rememberWindowState(
            placement = WindowPlacement.Floating,
            position = WindowPosition(Alignment.Center),
            size = DpSize(300.dp, 100.dp)
        ),
        title = TITLE,
        undecorated = true,
        transparent = false,
    ) {
        Box(modifier = Modifier.fillMaxSize()) {
            loading(Modifier.align(Alignment.Center))
        }
    }
}

@Composable
@OptIn(ExperimentalComposeUiApi::class)
fun ApplicationScope.main() {
    val ws = rememberWindowState(
        placement = WindowPlacement.entries[configWindowPlacement],
        position = WindowPosition(configWindowPositionX.dp, configWindowPositionY.dp),
        size = DpSize(configWindowSizeWidth.dp, configWindowSizeHeight.dp),
    )
    LaunchedEffect(ws.placement, ws.position, ws.size) {
        configWindowPlacement = ws.placement.ordinal
        if (ws.placement == WindowPlacement.Floating) {
            configWindowPositionX = ws.position.x.value
            configWindowPositionY = ws.position.y.value
            configWindowSizeWidth = ws.size.width.value
            configWindowSizeHeight = ws.size.height.value
        }
    }
    Window(
        onCloseRequest = ::exitApplication,
        title = TITLE,
        state = ws,
        icon = painterResource("main.ico", ResourceLoader.Default)
    ) {
        val s = remember { GlobalState }
        LaunchedEffect(s.authorized) {
            if (!s.authorized) {
                navigation.goto(HOME)
            }
        }
        Box(Modifier.fillMaxSize()) {
            navigation.show(Modifier.fillMaxSize())
            MessageContent().show(Modifier.fillMaxWidth().height(50.dp).align(Alignment.BottomCenter))
        }
    }
}